
//
//  rightTableViewController.swift
//  weather
//
//  Created by 陈思斌 on 21/8/17.
//  Copyright © 2017年 陈思斌. All rights reserved.
//

import UIKit

class RightTableViewController: UITableViewController {
    var controller: UIViewController?
    var historyCity = Helper.readCities()
    var section0Title = ["提醒","设置","支持"]
    let section0Image = ["reminder","setting_right","contact"]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = leftControllerAndRightControllerBgColor
        let nib = UINib(nibName: "RightTableViewCell", bundle: Bundle.main)
        self.tableView.register(nib, forCellReuseIdentifier: "reuseIdentifier")
        
        self.tableView.rowHeight = 70
        self.tableView.separatorStyle = .none
        NotificationCenter.default.addObserver(self, selector: #selector(reloadCities), name: NSNotification.Name(rawValue: AutoLocationNotification), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(reloadCities), name: NSNotification.Name(rawValue:ChooseLocationCityNotification), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(reloadCities), name: NSNotification.Name(rawValue:deleteLocationCityNotification), object: nil)
    }
    func reloadCities(sender:Notification){
        self.historyCity = Helper.readCities()
       
        self.tableView.reloadData()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
       
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return 3
        }else{
            return 2 + historyCity.count
        }
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! RightTableViewCell
        cell.controller = self
        if indexPath.section == 0{
            cell.titleLabel.text = section0Title[indexPath.row]
            cell.indicateImageView.image = UIImage(named: section0Image[indexPath.row])
            cell.deleteImageView.isHidden = true
        }else{
            if indexPath.row == 0{
                cell.titleLabel.text = "添加"
                cell.indicateImageView.image = UIImage(named: "addcity")
                cell.deleteImageView.isHidden = true
            }else if indexPath.row == 1{
                cell.titleLabel.text = "定位"
                cell.indicateImageView.image = UIImage(named: "city")
                cell.deleteImageView.isHidden = true
            }else{
                cell.titleLabel.text = historyCity[indexPath.row - 2]
                cell.indicateImageView.image = UIImage(named: "city")
                cell.deleteImageView.isHidden = false
            }
            
            
        }
        
        return cell
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0{
            return CGFloat(0)
        }else{
            return CGFloat(30)
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0{
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 0))
            return label
        }else{
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 30))

            label.text = "城市管理"
            label.textAlignment = .center
            label.textColor = UIColor.white
            label.backgroundColor = UIColor.black
            return label
        }
        
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1{
            if indexPath.row == 0{
                let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
                let addNewCityController = storyBoard.instantiateViewController(withIdentifier: "AddNewCityTableViewController") as! AddNewCityTableViewController
                self.controller?.present(addNewCityController, animated: true, completion: { 
                    
                })
                
            }else if indexPath.row == 1{
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: AutoLocationNotification), object: nil)

            }else{
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: ChooseLocationCityNotification), object: nil, userInfo: ["choose_city":self.historyCity[indexPath.row - 2]])
            }
        }
    }
}
